<?php

namespace app\admin\controller\hr;

use app\common\controller\Backend;
use think\Db;
use think\Exception;

/**
 * HR请假管理
 */
class Leave extends Backend
{
    protected $model = null;
    
    public function _initialize()
    {
        parent::_initialize();
        $this->model = Db::name('hr_leave');
    }
    
    /**
     * 查看请假列表
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            $total = $this->model
                ->alias('l')
                ->join('user u', 'l.user_id = u.id', 'LEFT')
                ->where($where)
                ->count();
            
            $list = $this->model
                ->alias('l')
                ->join('user u', 'l.user_id = u.id', 'LEFT')
                ->field('l.*, u.nickname as user_name')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            
            // 格式化数据
            foreach ($list as &$item) {
                $item['start_time_text'] = $item['start_time'] ? date('Y-m-d H:i', $item['start_time']) : '';
                $item['end_time_text'] = $item['end_time'] ? date('Y-m-d H:i', $item['end_time']) : '';
                $item['leave_type_text'] = $this->getLeaveTypeText($item['leave_type']);
                $item['status_text'] = $this->getStatusText($item['status']);
            }
            
            $result = ['total' => $total, 'rows' => $list];
            return json($result);
        }
        
        return $this->view->fetch();
    }
    
    /**
     * 审批请假
     */
    public function approve($ids = null)
    {
        $row = $this->model->where('id', $ids)->find();
        
        if (!$row) {
            $this->error('记录不存在');
        }
        
        if ($this->request->isPost()) {
            $params = $this->request->post();
            
            $data = [
                'status' => $params['status'],
                'approver_id' => $this->auth->id,
                'approval_time' => time(),
                'approval_remark' => $params['approval_remark'] ?? '',
                'updatetime' => time()
            ];
            
            $this->model->where('id', $ids)->update($data);
            $this->success('审批成功');
        }
        
        // 获取用户信息
        $user = Db::name('user')->where('id', $row['user_id'])->find();
        $row['user_name'] = $user['nickname'] ?? '';
        $row['start_time_text'] = date('Y-m-d H:i', $row['start_time']);
        $row['end_time_text'] = date('Y-m-d H:i', $row['end_time']);
        $row['leave_type_text'] = $this->getLeaveTypeText($row['leave_type']);
        
        $this->view->assign('row', $row);
        return $this->view->fetch();
    }
    
    /**
     * 请假详情
     */
    public function detail($ids = null)
    {
        $row = $this->model
            ->alias('l')
            ->join('user u', 'l.user_id = u.id', 'LEFT')
            ->field('l.*, u.nickname as user_name, u.mobile')
            ->where('l.id', $ids)
            ->find();
        
        if (!$row) {
            $this->error('记录不存在');
        }
        
        // 格式化数据
        $row['start_time_text'] = date('Y-m-d H:i', $row['start_time']);
        $row['end_time_text'] = date('Y-m-d H:i', $row['end_time']);
        $row['leave_type_text'] = $this->getLeaveTypeText($row['leave_type']);
        $row['status_text'] = $this->getStatusText($row['status']);
        
        // 审批人信息
        if ($row['approver_id']) {
            $approver = Db::name('admin')->where('id', $row['approver_id'])->find();
            $row['approver_name'] = $approver['username'] ?? '';
        }
        
        $this->view->assign('row', $row);
        return $this->view->fetch();
    }
    
    /**
     * 获取请假类型文本
     */
    private function getLeaveTypeText($type)
    {
        $typeMap = [
            1 => '事假',
            2 => '病假',
            3 => '年假',
            4 => '调休',
            5 => '婚假',
            6 => '产假',
            7 => '丧假',
            8 => '其他'
        ];
        return $typeMap[$type] ?? '未知';
    }
    
    /**
     * 获取状态文本
     */
    private function getStatusText($status)
    {
        $statusMap = [
            0 => '待审批',
            1 => '已批准',
            2 => '已拒绝'
        ];
        return $statusMap[$status] ?? '未知';
    }
}
